programming4us
           
 
 
Windows Phone

Developing Applications for Windows Phone 7 : Visual Containers

- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019
11/27/2010 11:28:29 AM
If you consider the examples that have been shown, you may have missed the importance of XAML containers. The most obvious of these can be seen in the Grid element:
<UserControl x:Class="WinningTheLottery.Sample"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid>
<TextBlock Text="Hello" />
</Grid>
</UserControl>

The purpose of these containers is to allow other elements to be laid out in particular ways on the visual surface of Silverlight. The containers themselves typically don’t have any user interface themselves but simply are used to determine how different XAML elements are formatted on the screen. There are a number of layout containers that are important to designing in XAML. Each of these layout containers can take multiple child elements and lay them out in specific ways. You can see the common visual containers in Table 1.

Table 1. Visual Containers
Layout ContainerDescriptionSupports Multiple Children?
GridTable-like layout of columns and rows.Yes
StackPanelHorizontal or Vertical stacking of individual elements.Yes
CanvasPosition-based layout (via Top and Left position).Yes
ScrollViewerVirtual container that can be larger than the contents to allow users to scroll through the container.No
BorderTo create a simple border around a single element.No

These containers are important as they are used to determine how your elements are laid out. The most important of these is the Grid container and it will be the one you use most often. The Grid is a container that supports dynamic, table-like layout using rows and columns. To define rows and columns, you set the Grid’s

<UserControl x:Class="WinningTheLottery.Sample"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<TextBlock Text="Hello" />
</Grid>
</UserControl>

Creating new columns and rows is done using the ColumnDefinitions and RowDefintions properties (as shown). This allows you to specify that individual elements are in a particular row or column using the attached property (see sidebar):

<UserControl x:Class="WinningTheLottery.Sample"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<TextBlock Text="Hello" Grid.Column="1" />
</Grid>
</UserControl>

By using the attached property, the TextBlock is indicating that the TextBlock belongs in the second column (note that row and column numbers are zero indexed). In this way the Grid is creating columns or rows proactively by specifying the number of rows or columns up front. At first blush it may seem verbose to create row and/or column definitions this way, but its important as the definitions contain other important information that can be set.

What Are Attached Properties?

Some properties are not relevant until a property except in some specific scopes. For example, when an object is inside a Grid, being able to tell the XAML what column or row you are in becomes critical. But that same element inside a StackPanel has no notion of a row or column. Attached properties are specific types of properties that are only valid in certain cases. Attached Properties are defined by the name of the owning type and the name of the attached property (e.g. Grid.Row). The information in attached properties are available to the class that exposes them as that is where they are typically used.

For example in the Grid class, as the Grid lays out the elements inside of it, it will query for the attached property to determine which row and/or column to place an element. Literally the properties are attached at runtime so the underlying element does not need to have unneeded properties (like Row and Column).


When creating Rows and Columns, the Height or Width (respectively) can be defined in three ways as seen in Table 2.

Table 2. Grid Row and Column Sizing
TypeDescriptionExample
AutoSizes row or column based the contents. The size will be determined by the largest object in a respective row or column.<RowDefinition Height=“Auto” />
PixelSet size of row and column to a specific size, in pixels. Larger objects will be clipped.<RowDefinition Height=“100” />
StarProportionally sizes rows or columns based on the weighted value.
<RowDefinition Height=“*” />
<RowDefinition Height=“25*” />
<RowDefinition Height=“0.147*” />


Auto and pixel sized are pretty self-explanatory, but the star sizing required some more explanation. Star sized is a proportionally sized based on the values in the height or width. For example:

<UserControl x:Class="WinningTheLottery.Sample"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="33*" />
<ColumnDefinition Width="66*" />
</Grid.ColumnDefinitions>
</Grid>
</UserControl>

The numbers in the Width are used as weighted proportions of the whole size. While this looks similar to using percentages (like you may be used to in web applciations), the numbers are not part of an arbitrary 100% scale. For example, changing the values to "1*" and "2*" will yield the same 2-to-1 ratio as "33*" and "66*". In the case of using a star alone (e.g. “*”), it is equivalent of "1*". By using Grid elements with a mix of Auto, pixel and star sizing, you can create elastic layouts (using star sizing for the flexible sized elements and pixel/auto sizing for the more static parts of the design).

You have already seen that you can use attached properties to set the row and/or column of a specific element inside the Grid. The Grid class also supports being able to specify RowSpan and ColumnSpan to signify that a particular element should span more than one row and/or column. This will give you extra flexibility to create your table-based designs, like so:

<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<TextBlock Text="1" />
<TextBlock Text="1"
Grid.Row="1" />
<TextBlock Text="1"
Grid.Column="1" />
<TextBlock Text="Across All 3 Columns"
Grid.ColumnSpan="3" />
<TextBlock Text="Across Both Rows"
Grid.RowSpan="2" />
</Grid>

While you may use the other layout containers is certain cases, you should become comfortable with the Grid as it is the container you will use most often.

Other -----------------
- Developing Applications for Windows Phone 7 : What is XAML?
- Windows Phone 7 : Connecting a Bluetooth Headset
- Windows Phone 7 : Turning On Airplane Mode
- Windows Phone 7 : Updating Your Phone Software
- Windows Phone 7 : Finding a Lost Phone
- Windows Phone 7 : Locking Your Phone
- Windows Phone 7 : Importing Contacts from a SIM Card
- Windows Phone 7 : Silencing Your Phone
- Windows Phone 7 with Silverlight : Working with the Phone
- Writing Your First Phone Application - Adding Code (part 2)
- Writing Your First Phone Application - Adding Code (part 1)
- Developing Applications for Windows Phone 7 : Designing with Blend
- Developing Applications for Windows Phone 7 : Creating a New Project
- Developing Applications for Windows Phone 7 with Silverlight : Preparing Your Machine
- Windows Phone 7 : Picking Ringtones and Alerts
- Windows Phone 7 : Changing Themes and Wallpaper
- Windows Phone 7 : Customizing the Start Screen
- Windows Phone 7 : Connecting to a Wi-Fi Hotspot
- Windows Phone 7 : Setting Up Facebook
- Windows Phone 7 : Setting Up E-Mail and Your Calendar
 
 
 
Top 10
 
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
- First look: Apple Watch

- 3 Tips for Maintaining Your Cell Phone Battery (part 1)

- 3 Tips for Maintaining Your Cell Phone Battery (part 2)
programming4us programming4us